package com.dayuan.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dayuan.bean.Cart;
import com.dayuan.bean.CartInfo;
import com.dayuan.dao.CartInfoMapper;
import com.dayuan.dao.CartMapper;

@Service
public class CartService {
	private static Logger logger = LoggerFactory.getLogger(CartService.class);

	@Resource
	private CartMapper cartMapper;

	@Resource
	private CartInfoMapper cartInfoMapper;

	public Cart selectByUid(Long uid) {
		return cartMapper.selectByUid(uid);
	}

	/**
	 * 添加购物车
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void addCart(Long uid, Long goodsId, Integer goodsNum) {

		Cart cart = cartMapper.selectByUid(uid);
		if (cart == null) {
			cart = new Cart(uid);
			cartMapper.insert(cart);
		}
		cartInfoMapper.insert(new CartInfo(goodsId, goodsNum, cart.getId()));
	}

}
